package jp.dev7.enchant.tot.controller;

import java.io.PrintWriter;

import net.arnx.jsonic.JSON;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public abstract class AbsJsonpController extends Controller {

    protected PrintWriter out;

    @Override
    final protected Navigation run() throws Exception {
        response.setContentType("text/javascript");
        response.setCharacterEncoding("UTF-8");
        out = response.getWriter();
        try {

            out.print(asString("callback"));
            out.print("(");
            out.print(JSON.encode(exec()));
            out.print(")");
            response.flushBuffer();

        } finally {
            out.close();
        }

        return null;
    }

    protected abstract Object exec() throws Exception;

}
